package net.sxnic.jhitem.itemoperation.taglib;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import net.sxnic.item.ItemConstants;
import net.sxnic.jhitem.itemoperation.ItemOperation;

import org.apache.commons.lang.StringUtils;

public class OperationListSelectTag extends BodyTagSupport{

	private static final long serialVersionUID = 5304584564438493407L;
	
	private List<ItemOperation> list;
	
	private String cssClass;

	private String name;

	private String value;

	// onchange事件
	private String onChange;

	// 默认第一个的key值
	private String headerKey;

	// 默认第一个的value值
	private String headerValue;
	
	public int doEndTag() {
		JspWriter out = pageContext.getOut();
	
		try {
			if (list == null || list.size() <= 0) {
				out.print("null list");
			} else {
				if (StringUtils.isBlank(name)) {
					out.print("error select name");
					return EVAL_PAGE;
				}

				StringBuffer sb = new StringBuffer();
				sb.append("<select id='" + name + "_id' name='" + name + "' ");

				if (StringUtils.isNotBlank(onChange)) {
					sb.append(" onchange=\"" + onChange + "\" ");
				}

				if (StringUtils.isNotBlank(cssClass)) {
					sb.append(" class=\"" + cssClass + "\" ");
				}

				sb.append(" >");

				if (StringUtils.isNotBlank(headerKey)
						|| StringUtils.isNotBlank(headerValue)) {
					sb.append("<option value='" + headerKey + "'>"
							+ headerValue + "</option>");
				}

				for (ItemOperation operation : list) {
					if (operation.getSoperCode().equals(value)) {
						sb.append("<option value='" + operation.getSoperCode() + "' selected>"
								+ operation.getSoperShortname() + "</option>");
					} else {
						sb.append("<option value='" + operation.getSoperCode() + "'>"
								+ operation.getSoperShortname() + "</option>");
					}
				}

				sb.append("</select>");

				out.print(sb.toString());

			}
			out.print("");

		} catch (IOException e) {

		}
		
		
		return EVAL_PAGE;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getOnChange() {
		return onChange;
	}

	public void setOnChange(String onChange) {
		this.onChange = onChange;
	}

	public String getHeaderKey() {
		return headerKey;
	}

	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	public String getHeaderValue() {
		return headerValue;
	}

	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}

	public List<ItemOperation> getList() {
		return list;
	}

	public void setList(List<ItemOperation> list) {
		this.list = list;
	}

}
